package com.anddict.sql;

import com.anddict.util.MiscUtil;
import com.anddict.util.Reference;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DbAdapter {

	private SQLiteDatabase database;
	
	private String pathDatabase;
	
	// simply check database existence
	public boolean isDatabaseOpen(){
		if (database == null || database.isOpen() == false){
			return false;
		}
		return true;
	}
	
	public String getTableName(String firstChar){
		try{
			int ret = Integer.parseInt(firstChar);
			return "nums";
		}catch(NumberFormatException ex){
			return firstChar.toLowerCase();
		}
	}
	
	public String[] searchLikeTerms(String term, int limit){
		
		Log.d(Reference.TAG, "DbAdapter.searchLikeTerms() -term " + term + " -limit " + limit);

		if (this.isDatabaseOpen() == false){
			Log.d(Reference.TAG, "DbAdapter.searchLikeTerms() - no database");
			return null;
		}
		
		if (term == null || term.equals("")){
			Log.d(Reference.TAG, "DbAdapter.searchLikeTerms() - no term");
			return null;
		}
		
		String tableName = this.getTableName("" + term.charAt(0));
		
		String query = "select term from " + tableName + " where lower(term) like '" + term.toLowerCase().trim() + "%'";
		
		if (limit > 0){
			query += " limit " + limit;
		}
		
		try{
			Cursor c = database.rawQuery(query,null);
			
			// check cursor is not null or cursor is not empty
			if (c == null || c.isBeforeFirst() == false){
				Log.d(Reference.TAG, "DbAdapter.searchLikeTerms() - cursor is null or empty");
				return null;
			}
			
			String[] terms = new String[c.getCount()];
			
			for (int x = 0; c.moveToNext() == true; x++){	
				terms[x] = c.getString(0);
			}
	
			// close cursor
			c.close();
			
			return terms;

		}catch(SQLException ex){
			Log.e(Reference.TAG, MiscUtil.getStackTrace(ex));
			return null;
		}
	}
	
	public String getTranslate(String term){
		
		if (this.isDatabaseOpen() == false){
			Log.d(Reference.TAG, "DbAdapter.getTranslate() - no database");
			return null;
		}
		
		if (term == null || term.equals("")){
			Log.d(Reference.TAG, "DbAdapter.getTranslate() - no term");
			return null;
		}
		
		String tableName = this.getTableName("" + term.charAt(0));
		
		try{
			Cursor c = database.rawQuery("select translation from " + tableName + " where lower(term) = ?",new String[]{term.toLowerCase().trim()});
			
			// check cursor is not null or cursor is not empty
			if (c == null || c.isBeforeFirst() == false){
				Log.d(Reference.TAG, "DbAdapter.getTranslate() - cursor is null or empty");
				return null;
			}
			
			c.moveToNext();
			String translate = c.getString(0); 
	
			// close cursor
			c.close();
			
			return translate;

		}catch(SQLException ex){
			Log.e(Reference.TAG, MiscUtil.getStackTrace(ex));
			return null;
		}
	}
	
	public boolean getTerm(String tableName, String term){
		
		if (this.isDatabaseOpen() == false){
			Log.d(Reference.TAG, "DbAdapter.getTerm() - no database");
			return false;
		}
		
		if (tableName == null || tableName.equals("")){
			Log.d(Reference.TAG, "DbAdapter.getTerm() - no table name");
			return false;
		}
		
		if (term == null || term.equals("")){
			Log.d(Reference.TAG, "DbAdapter.getTerm() - no term");
			return false;
		}
		
		try{
			Cursor c = database.rawQuery("select 1 from " + tableName + " where lower(term) = ?",new String[]{term.toLowerCase().trim()});
			
			// check cursor is not null or cursor is not empty
			if (c == null || c.isBeforeFirst() == false){
				Log.d(Reference.TAG, "DbAdapter.getTerm() - cursor is null or empty");
				return false;
			}
			
			if (c.getCount() != 0){
				c.close();
				return true;
			}else{
				c.close();
				return false;
			}
		}catch(SQLException ex){
			Log.e(Reference.TAG, MiscUtil.getStackTrace(ex));
			return false;
		}
		
	}
	
	public boolean checkTableExistence(String tableName){
		
		if (this.isDatabaseOpen() == false){
			Log.d(Reference.TAG, "DbAdapter.checkTableExistence() - no database");
			return false;
		}
		
		if (tableName == null || tableName.equals("")){
			Log.d(Reference.TAG, "DbAdapter.checkTableExistence() - no tableName");
			return false;
		}
		
		try{
			Cursor c = database.rawQuery("select 1 from sqlite_master where name = ?",new String[]{tableName.toLowerCase().trim()});
			
			// check cursor is not null or cursor is not empty
			if (c == null || c.isBeforeFirst() == false){
				Log.d(Reference.TAG, "DbAdapter.getTerm() - cursor is null or empty");
				return false;
			}
			
			if (c.getCount() != 0){
				c.close();
				return true;
			}else{
				c.close();
				return false;
			}
		}catch(SQLException ex){
			Log.e(Reference.TAG, MiscUtil.getStackTrace(ex));
			return false;
		}
	}
	
	public boolean openDatabase(String path){

		if (this.isDatabaseOpen() == true){
			Log.d(Reference.TAG, "DbAdapter.openDatabase - close database");
			database.close();
		}
		
		this.pathDatabase = path;

		Log.d(Reference.TAG, "DbAdapter.openDatabase - try to open database -path " + path);
		// no cursor factory for these first release ;)
		try{
			database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY + SQLiteDatabase.NO_LOCALIZED_COLLATORS);
			
			if (database == null || database.isOpen() == false){
				Log.d(Reference.TAG, "DbAdapter.openDatabase - impossible to open database");
				return false;
			}
			
		}catch (SQLException ex){
			Log.e(Reference.TAG, MiscUtil.getStackTrace(ex));
			return false;
		}
		
		return true;
	}
}
